<template>
  <div id="productCategroys">
    <!-- 面包导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>调拨管理</el-breadcrumb-item>
      <el-breadcrumb-item>调拨管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 右侧卡片区域 -->
    <!-- 用户列表卡片区 -->
    <el-card class="box-card">
      <el-form :inline="true" :model="queryMap" class="demo-form-inline">
        <el-form-item label="联系人">
          <el-input v-model="queryMap.name" clearable @clear="search" placeholder="联系人"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
          <el-button type="primary" icon="el-icon-circle-plus-outline" @click="openAdd">添加</el-button>
        </el-form-item>
      </el-form>
      <template>
        <el-table border v-loading="loading" stripe :data="consumerData" style="width: 100%;" height="440">
          <el-table-column prop="id" type="index" label="ID" width="50"></el-table-column>
          <el-table-column prop="adress" label="收获地址"></el-table-column>
          <el-table-column prop="pName" label="物资名称" width="100"></el-table-column>
          <el-table-column prop="pCount" label="物资数量" width="100"></el-table-column>
          <el-table-column prop="createTime" label="创建时间" width="100"></el-table-column>
          <el-table-column prop="receiver" label="联系人" width="100"></el-table-column>
          <el-table-column prop="phone" label="电话" width="142"></el-table-column>
          <el-table-column prop="isvaild" label="发货状态" width="80"></el-table-column>
          <el-table-column label="操作" fixed="right" width="120">
            <template slot-scope="scope">
              <el-button @click="openEditUser(scope.row)" type="text" size="mini" icon="el-icon-edit" >编辑</el-button>
              <el-button type="text" size="mini" icon="el-icon-delete" @click="del(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.page"
        :page-sizes="[5, 10, 15, 30]"
        :page-size="queryInfo.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <el-dialog title="新增调拨信息" :visible.sync="addDialogVisible" width="50%" @close="closeAdd">
      <el-form :model="addForm" :rules="rulesAdd" ref="ruleFormAdd" label-width="100px">
        <el-form-item label="收货地址" prop="adress">
          <el-input v-model="addForm.adress"></el-input>
        </el-form-item>
        <el-form-item label="收货人" prop="receiver">
          <el-input v-model="addForm.receiver"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="addForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="物资名称" prop="pName">
          <el-input v-model="addForm.pName"></el-input>
        </el-form-item>
        <el-form-item label="数量" prop="pCount">
          <el-input v-model="addForm.pCount"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addTransfer">确 定</el-button>
        </span>
    </el-dialog>
    <el-dialog title="修改调拨信息" :visible.sync="editDialogVisible" width="50%" @close="closeEdit" >
      <el-form :model="editForm" :rules="rulesAdd" ref="ruleFormEdit" label-width="100px">
        <el-form-item label="收货地址" prop="adress">
          <el-input v-model="editForm.adress"></el-input>
        </el-form-item>
        <el-form-item label="收货人" prop="receiver">
          <el-input v-model="editForm.receiver"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="editForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="物资名称" prop="pName">
          <el-input v-model="editForm.pName"></el-input>
        </el-form-item>
        <el-form-item label="数量" prop="pCount">
          <el-input v-model="editForm.pCount"></el-input>
        </el-form-item>
        <el-form-item label="发货状态" prop="isvaild">
          <el-input v-model="editForm.isvaild"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="edit">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    var checkMobile = (rule, value, callback) => {
      const phoneReg = /^1[34578]\d{9}$$/
      if (!value) {
        return callback(new Error('电话号码不能为空'))
      }
      setTimeout(() => {
        // Number.isInteger是es6验证数字是否为整数的方法,实际输入的数字总是识别成字符串
        // 所以在前面加了一个+实现隐式转换

        if (!Number.isInteger(+value)) {
          callback(new Error('请输入数字值'))
        } else {
          if (phoneReg.test(value)) {
            callback()
          } else {
            callback(new Error('电话号码格式不正确'))
          }
        }
      }, 100)
    }
    return {
      editForm: {},
      addForm: {
        adress: '',
        pName: '',
        pCount: '',
        receiver: '',
        phone: ''
      },
      queryInfo: {
        username: '',
        page: 1,
        size: 10
      },
      loading: true,
      editDialogVisible: false,
      addDialogVisible: false, // 添加弹框是否显示
      total: 0, // 总共多少条数据
      consumerData: [], // 表格数据
      queryMap: {
        pageNum: 1,
        pageSize: 10,
        name: ''
      }, // 查询对象
      addRuleForm: {}, // 添加表单数据
      deans: [], // 所有系主任
      rulesAdd: {
        receiver: [
          { required: true, message: '请输入用户姓名', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }],
        phone: [
          { required: true, message: '请输入电话', trigger: 'blur' },
          { validator: checkMobile, trigger: 'blur' }],
        address: [
          { required: true, message: '请输入收获地址', trigger: 'blur' }],
        pName: [
          { required: true, message: '请输物资名称', trigger: 'blur' }],
        pCount: [
          { required: true, message: '请输入调拨数量', trigger: 'blur' }],
        isvaild: [
          { required: true, message: '请输入发货状态', trigger: 'blur' }]
      },
      provinceList: [], // 省列表
      cityList: [], // 市列表
      originList: [], // 区列表
      cityOptions: [], // 市下拉框数据
      originOptions: [] // 区下拉框数据
    }
  },
  methods: {
    openEditUser (userInfo) {
      this.editForm = userInfo
      this.editDialogVisible = true
    },
    closeEdit () {
      this.$refs.ruleFormEdit.resetFields()
    },
    closeAdd () {
      this.$refs.ruleFormAdd.resetFields()
    },
    handleSizeChange (val) {
      this.queryInfo.size = val
      this.getConsumerList()
    },
    handleCurrentChange (val) {
      this.queryInfo.page = val
      this.getConsumerList()
    },
    /**
       * 打开添加弹出框
       */
    openAdd () {
      this.addDialogVisible = true
      this._getJsonData()
    },
    // 搜索
    search () {
      this.queryMap.pageNum = 1
      this.getConsumerList()
    },
    // 删除物资去处
    async del (id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete('transfer/' + id)
        if (res.code !== 200) {
          return this.$message.error(res.msg)
        }
        await this.getConsumerList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    /**
       * 编辑
       */
    async edit () {
      this._getJsonData()
      const { data: res } = await this.$http.put('transfer', this.editForm)
      if (res.code !== 200) {
        this.$message.error(res.msg)
        return false
      }
      this.$message.success(res.msg)
      this.editDialogVisible = false
      this.getConsumerList()
    },
    // 添加
    async addTransfer () {
      const { data: res } = await this.$http.post('transfer', this.addForm)
      if (res.code !== 200) {
        this.$message.error(res.msg)
        return false
      }
      this.$message.success(res.msg)
      await this.getUserList()
      this.addDialogVisible = false
    },
    /**
       * 加载物资去处列表
       */
    async getConsumerList () {
      const { data: res } = await this.$http.get('transfer', {
        params: this.queryMap
      })
      if (res.code !== 200) {
        return this.$message.error('获取用户列表失败')
      } else {
        this.total = res.data.total
        this.consumerData = res.data.result
      }
    },
    _getJsonData () {
      this.$http.get('/json/provinces.json').then(res => {
        this.provinceList = []
        this.cityList = []
        this.originList = []
        res.data.forEach(item => {
          if (item.value.match(/0000$/)) {
            this.provinceList.push({
              value: item.value,
              label: item.name,
              children: []
            })
          } else if (item.value.match(/00$/)) {
            this.cityList.push({
              value: item.value,
              label: item.name,
              children: []
            })
          } else {
            this.originList.push({
              value: item.value,
              label: item.name
            })
          }
        })
        for (const index in this.provinceList) {
          for (const index1 in this.cityList) {
            if (
              this.provinceList[index].value.slice(0, 2) ===
                this.cityList[index1].value.slice(0, 2)
            ) {
              this.provinceList[index].children.push(this.cityList[index1])
            }
          }
        }
        for (const item1 in this.cityList) {
          for (const item2 in this.originList) {
            if (
              this.originList[item2].value.slice(0, 4) ===
                this.cityList[item1].value.slice(0, 4)
            ) {
              this.cityList[item1].children.push(this.originList[item2])
            }
          }
        }
      })
    }
  },
  created () {
    this._getJsonData()
    this.getConsumerList()
    setTimeout(() => {
      this.loading = false
    }, 500)
  }
}
</script>
